<template>
  <div class="container">
    <el-card>
      <!-- 搜索区 -->
      <el-form :inline="true" :model="params" class="articles-form">
        <el-form-item label="关键字" class="inputBox">
          <el-input
            v-model="params.keyword"
            placeholder="根据文章标题搜索"
            size="small"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" class="selectBox">
          <el-select v-model="params.state" placeholder="请选择" size="small">
            <el-option label="启用" value="qiyong"></el-option>
            <el-option label="禁用" value="jinyong"></el-option>
          </el-select>
          <el-button size="small" class="filter-item" style="margin-left: 10px" @click="clearForm"
            >清除</el-button
          >
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            class="filter-item"
            @click="queryList"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-edit" @click="showAddDialog" size="small"
            >新增技巧</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 提示 -->
      <el-alert :title="'数据一共' + total + '条'" type="info" show-icon closable> </el-alert>
      <!-- 表格区 -->
      <el-table :data="articleList" style="width: 100%">
        <el-table-column type="index" label="序号" width="60"> </el-table-column>
        <el-table-column prop="title" label="文章标题" width="400">
          <template #default="{ row }">
            {{ row.title }} &nbsp;
            <el-button
              type="text"
              v-if="row.videoURL"
              class="el-icon-film"
              style="color: blue"
              @click="videoShow(row.videoURL)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读人数" width="100"> </el-table-column>
        <el-table-column prop="username" label="录入人" width="160"> </el-table-column>
        <el-table-column label="录入时间" width="160">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120">
          <template #default="{ row }">
            {{ formatState(row.state) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" @click="showView(row.id)">预览</el-button>
            <el-button type="text" @click="updateState(row.id, row.state)">
              {{ row.state ? '禁用' : '启用' }}
            </el-button>
            <el-button
              type="text"
              :disabled="row.state ? true : false"
              @click="showeditDialog(row.id)"
              >修改</el-button
            >
            <el-button type="text" @click="delArticle(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query.pagesize"
        layout=" prev, pager, next, sizes, jumper"
        :total="total"
        background
        style="margin: 20px; float: right"
      >
      </el-pagination>
    </el-card>
    <ArticlePreView
      ref="preViewRef"
      :preViewdialogVisible="preViewdialogVisible"
      @closeView="closeView"
    ></ArticlePreView>
    <ArticlesAdd
      :adddialog-visible="adddialogVisible"
      @closeDialog="closeDialog"
      @getList="getArticleList"
      @updateOne="query.page = 1"
      ref="articleRef"
    ></ArticlesAdd>
    <!-- 视频 -->
    <div class="video" v-if="flag">
      <div class="video-close" @click="closeVideo()">
        <i class="el-icon-close"></i>
      </div>
      <div class="video-box">
        <video autoplay :src="url"></video>
      </div>
    </div>
  </div>
</template>
<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import * as dayjs from 'dayjs'
import ArticlePreView from '@/module-hmmm/components/articles-preview'
import ArticlesAdd from '@/module-hmmm/components/articles-add.vue'
export default {
  components: { ArticlePreView, ArticlesAdd },
  data () {
    return {
      closable: false,
      articleList: [],
      query: {
        page: 1,
        pagesize: 15
      },
      params: {
        keyword: '',
        state: null
      },
      total: 0,
      preViewdialogVisible: false,
      adddialogVisible: false,
      flag: false,
      url: ''
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const { data } = await list(this.query)
      // console.log(data)
      this.articleList = data.items
      this.total = data.counts
    },
    formatDate (val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    },
    formatState (state) {
      const map = {
        0: '已禁用',
        1: '已启用'
      }
      return map[state]
    },
    closeView () {
      this.preViewdialogVisible = false
    },
    showView (id) {
      this.preViewdialogVisible = true
      this.$refs.preViewRef.getViewDetail(id)
    },
    async updateState (id, state) {
      state = state === 0 ? 1 : 0
      // console.log(state)
      await changeState({
        id: id,
        state: state
      })
      this.getArticleList()
      this.$message.success('修改成功')
    },
    async showeditDialog (id) {
      this.adddialogVisible = true
      this.$refs.articleRef.getArticleDetail(id)
    },
    showAddDialog () {
      this.adddialogVisible = true
    },
    closeDialog () {
      this.adddialogVisible = false
    },
    delArticle (id) {
      this.$confirm('是否删除改文章', '温馨提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除成功')
        this.getArticleList()
      })
    },
    handleSizeChange (size) {
      this.query.pagesize = size
      this.query.page = 1
      this.getArticleList()
    },
    handleCurrentChange (page) {
      this.query.page = page
      this.getArticleList()
    },
    clearForm () {
      this.params.keyword = ''
      this.params.state = null
      this.queryList()
    },
    async queryList () {
      const { data } = await list(this.params)
      this.articleList = data.items
    },
    // 视频播放
    videoShow (url) {
      // console.log(url)
      this.url = url
      this.flag = true
    },
    // 关闭视频
    closeVideo () {
      this.flag = false
    }
  }
}
</script>

<style scoped lang='less'>
.el-card {
  margin: 10px;
  .el-alert {
    margin-bottom: 15px;
  }
}
.articles-form {
  display: flex;
  justify-content: space-between;
  height: 50px;
  margin-left: 18px;
  line-height: 35px;
  .inputBox {
    flex: 0.5;
  }
  .selectBox {
    flex: 1.5;
  }
}
.video {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;

  .video-close {
    width: 50px;
    height: 50px;
    line-height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 25px;
    border-radius: 50%;
    text-align: center;
  }

  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
